/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.enroll;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.modules.edu.entity.course.EduCourse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.common.utils.excel.ExportExcel;
import com.idenger.modules.edu.entity.enroll.EduEnroll;
import com.idenger.modules.edu.entity.project.EduProject;
import com.idenger.modules.edu.service.enroll.EduEnrollService;
import com.idenger.modules.edu.service.user.EduUserService;

/**
 * 报名记录Controller
 * @author 兰正军
 * @version 2017-12-12
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/enroll/eduEnroll")
public class EduEnrollController extends BaseController {

	@Autowired
	private EduEnrollService eduEnrollService;
	@Autowired
	private EduUserService eduUserService;
	
	
	@ModelAttribute
	public EduEnroll get(@RequestParam(required=false) String id) {
		EduEnroll entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduEnrollService.get(id);
		}
		if (entity == null){
			entity = new EduEnroll();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:enroll:eduEnroll:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduEnroll eduEnroll, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduEnroll> page = eduEnrollService.findPage(new Page<EduEnroll>(request, response), eduEnroll); 
		model.addAttribute("page", page);
		return "modules/edu/enroll/eduEnrollList";
	}

	@RequiresPermissions("edu:enroll:eduEnroll:view")
	@RequestMapping(value = "form")
	public String form(EduEnroll eduEnroll, Model model) {
		model.addAttribute("eduEnroll", eduEnroll);
		return "modules/edu/enroll/eduEnrollForm";
	}

	@RequiresPermissions("edu:enroll:eduEnroll:edit")
	@RequestMapping(value = "save")
	public String save(EduEnroll eduEnroll, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eduEnroll)){
			return form(eduEnroll, model);
		}
		eduEnrollService.save(eduEnroll);
		addMessage(redirectAttributes, "保存报名记录成功");
		return "redirect:"+Global.getAdminPath()+"/edu/enroll/eduEnroll/?repage";
	}
	
	@RequiresPermissions("edu:enroll:eduEnroll:edit")
	@RequestMapping(value = "delete")
	public String delete(EduEnroll eduEnroll, RedirectAttributes redirectAttributes) {
		eduEnrollService.delete(eduEnroll);
		addMessage(redirectAttributes, "删除报名记录成功");
		return "redirect:"+Global.getAdminPath()+"/edu/enroll/eduEnroll/?repage";
	}
	
	/**
	 * 该项目下的学员列表
	 * @param eduCourse
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "courseByUserList")
	public String courseByUserList(EduCourse eduCourse, HttpServletRequest request, HttpServletResponse response, Model model){
		EduEnroll eduEnroll = new EduEnroll();
		eduEnroll.setCurriculumId(eduCourse.getId());
		String status = request.getParameter("status");
		eduEnroll.setStatus(status);
		List<EduEnroll> list = eduEnrollService.findUserListForCourseId(eduEnroll);
		model.addAttribute("list", list);
		model.addAttribute("title","课程");
		model.addAttribute("businessId", eduCourse.getId());
		model.addAttribute("status",status);
		return "modules/edu/enroll/enrollUserList";
	}
	/***
	 * 查询该项目下的学员列表
	 * @param eduProject
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "projectByUserList")
	public String projectByUserList(EduProject eduProject,HttpServletRequest request, HttpServletResponse response, Model model){
		EduEnroll eduEnroll = new EduEnroll();
		eduEnroll.setProjectId(eduProject.getId());
		List<EduEnroll> list = eduEnrollService.findUserListForProjectId(eduEnroll);
		List<EduEnroll> eduEnrolls = new ArrayList<>();
		for(EduEnroll eduEnroll2 :list){
			String isStatus = endProjectStatus(eduEnroll2);
			eduEnroll2.setStatus(isStatus);
			eduEnrolls.add(eduEnroll2);
		}
		model.addAttribute("list", eduEnrolls);
		model.addAttribute("title", "项目");
		model.addAttribute("businessId",eduProject.getId());
		return "modules/edu/enroll/enrollUserList";
	}
	
	public String endProjectStatus(EduEnroll eduEnroll){
		EduEnroll eduEnroll2 = new EduEnroll();
		eduEnroll2.setUserid(eduEnroll.getUserid());
		eduEnroll2.setProjectId(eduEnroll.getProjectId());
		List<EduEnroll> list = eduEnrollService.findList(eduEnroll2);
		boolean isStatus = true;
		for(EduEnroll eduEnroll3 :list){
			if(!"1".equals(eduEnroll3.getStatus())){
				isStatus = false;
			}
		}
		if(isStatus){
			return "1";
		}
		return "2";
	}
	
	/***
	 * 导出课程下的学员信息
	 * @param eduEnroll
	 * @param redirectAttributes
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "exportCourseUserList")
	public String  exportCourseUserList(EduEnroll eduEnroll,RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response, Model model){
		try {
		List<EduEnroll> list = eduEnrollService.findUserListForCourseId(eduEnroll);
		if(list.size() >0){
			eduEnroll = list.get(0);
		}
		String fileName = "学员列表"+new Date().getTime()+".xlsx";
		ExportExcel exportExcel = new ExportExcel("课程名称:"+eduEnroll.getName(),  EduEnroll.class);
		exportExcel.setDataList(list);
		exportExcel.write(response, fileName);
		exportExcel.dispose();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/***
	 * 导出项目下的学员信息   
	 * @param eduEnroll
	 * @param redirectAttributes
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "exportProjectUserList")
	public String exportProjectUserList(EduEnroll eduEnroll,RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response, Model model){
		try {
			List<EduEnroll> list = eduEnrollService.findUserListForProjectId(eduEnroll);
			if(list.size()>0){
				eduEnroll = list.get(0);
			}
			String fileName = "学员列表"+new Date().getTime()+".xlsx";
			ExportExcel exportExcel = new ExportExcel("项目名称:"+eduEnroll.getName(),  EduEnroll.class);
			exportExcel.setDataList(list);
			exportExcel.write(response, fileName);
			exportExcel.dispose();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}